import axios from '@/utils/axios/axios';
import router from '@/router/router';
import type { MessageHandler, MessageParams } from 'element-plus';

/**
 * 网络请求
 */
export const $axios = axios;

/**
 * 路由配置
 */
export const $router = router;

/**
 * toast弹出框
 */
export const ElToast = {
  _show: (message: string, type: 'error' | 'info' | 'success' | 'warning', duration = 3000): MessageHandler => {
    return ElMessage({ showClose: true, grouping: true, message, type, duration });
  },
  show: (obj: MessageParams): MessageHandler => ElMessage(obj),
  info: (message: string, duration?: number) => ElToast._show(message, 'info', duration),
  success: (message: string, duration?: number) => ElToast._show(message, 'success', duration),
  warning: (message: string, duration?: number) => ElToast._show(message, 'warning', duration),
  error: (message: string, duration?: number) => ElToast._show(message, 'error', duration),
};

/**
 * 全屏加载动画
 */
export const ElFullLoading = {
  loadingInstance: null as { [key: string]: unknown; close: () => void } | null,
  show(text: string, options = {}) {
    this.loadingInstance = ElLoading.service({
      lock: true,
      fullscreen: true,
      ...options,
      text,
    });
  },
  hide() {
    this.loadingInstance && this.loadingInstance.close();
  },
};
